<?php

namespace App\Jobs;

use App\Models\Examination;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ChangeExaminationStatus implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;


    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $examination = Examination::query()
            // 开始时间 考试时长， 截止进场时间
            ->select(['id','start_time', 'timer', 'over_time', 'status'])
            ->where('status', '<', 2)
            // 开始时间 小于等于 现在时间
            ->where('start_time', '<=', Carbon::now())
            ->get();

        foreach ($examination as $item) {
            $item->status = 1;
            // 开始时间加结束时长
            $endTime = Carbon::parse($item->start_time)->addSeconds($item->timer);

            if (Carbon::now()->gte($endTime)) {
                $item->status = 2;
            }

            $item->save();
        }
    }
}
